package ui.common
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextFieldAutoSize;

	import globals.Global;
	import globals.Setting;

	import ui.component.GameTextField;

	import util.DisplayUtil;

	/**
	 * 提示框。
	 * @author lu-guobin
	 */
	public class ToolTipWindown extends Sprite
	{
		private var _myText:GameTextField; //主体内容部分。
		private var _width:Number;
		private var _height:Number;
		private var _title:GameTextField; //标题部分。

		public function ToolTipWindown()
		{
			init();
		}

		private function init():void
		{
			_myText = new GameTextField();
			_myText.defaultTextFormat = GameTextField.getTextFormat();
			_myText.x = 5;
			_myText.y = 5;
			_myText.wordWrap = _myText.multiline = true;
			this.addChild(_myText);
			_title = new GameTextField();
			_title.defaultTextFormat = GameTextField.getTextFormat();
			_title.autoSize = TextFieldAutoSize.LEFT;
		}

		private function reDrawBg():void
		{
			with(this.graphics)
			{
				clear();
				lineStyle(1, 0xababab);
				beginFill(0x000000, 0.6);
				drawRect(0, 0, _width, _height);
				endFill();
			}
		}

		public function showTipWithPic(textStr:String, picData:DisplayObject, posX:Number = 0, posY:Number = 0, width:Number = 100, height:Number = 0):void
		{

		}

		public function showHtml(htmlStr:String, width:Number = 150, height:Number = 0, numInRow:uint = 0, title:String = ''):void
		{
			if(title != '' && title != null)
			{
				_title.myHtmlText = title;
				_title.y = 5;
				_myText.y = _title.y + _title.height + 5;
				DisplayUtil.addChild(_title, this);
			}
			else
			{
				DisplayUtil.removeChild(this, _title);
				_myText.y = 5;
			}

			if(numInRow > 0)
			{
				width = numInRow * 12.5; //12号字体的宽就是12个像素。
			}
			else if(width == 0)
			{
				width = 150;
			}
			_myText.width = width;
			_myText.myHtmlText = htmlStr;
			//_myText.width = _myText.textWidth + 4;
			_width = _myText.width + 10;
			_height = _myText.y + _myText.height + 5;
			_title.x = (_width - _title.textWidth) / 2 - 2;
			open();
		}

		public function showText(textStr:String, width:Number = 150):void
		{
			if(width == 0)
			{
				width = 150;
			}
			DisplayUtil.removeChild(this, _title);
			_myText.width = width;
			_myText.mytext = textStr;
			_width = _myText.width + 10;
			_height = _myText.height + 10;
			_myText.y = 5;
			open();
		}

		private function open():void
		{
			reDrawBg();

			if(Global.stage.mouseX + _width > Setting.STAGE_WIDTH) //判断出右边的边界了.
			{
				this.x = Setting.STAGE_WIDTH - _width - 1;
			}
			else
			{
				this.x = Global.stage.mouseX;
			}

			if(Global.stage.mouseY + _height > Setting.STAGE_HEIGHT) //判断出下边的边界了.
			{
				this.y = Setting.STAGE_HEIGHT - _height - 1;
			}
			else
			{
				this.y = Global.stage.mouseY;
			}
			DisplayUtil.addChild(this, Global.game.topLayer);
		}

		public function close():void
		{
			DisplayUtil.removeChild(Global.game.topLayer, this);
		}
	}
}